package cn.itlaobing.web.tag;



import cn.itlaobing.web.model.ListModel;
import cn.itlaobing.web.model.PaginatoinModel;
import org.apache.log4j.Logger;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.SimpleTagSupport;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class PaginationTag<T> extends SimpleTagSupport{
    private Logger logger=Logger.getLogger(PaginationTag.class);
    private PaginatoinModel<T> paging;
    private static final String REQUEST_URI_ATTRIBUTE_KEY="javax.servlet.forward.request_uri";

    public void setPaging(PaginatoinModel<T> paging) {
        this.paging = paging;
    }

    @Override
    public void doTag() throws JspException, IOException {
        JspWriter jw=this.getJspContext().getOut();
        StringBuilder sb=new StringBuilder();
        sb.append("<ul class='pagination pagination-lg'>");
        previousPage(sb);//上一页
        everyPage(sb);//当前页
        nextPage(sb);//下一页
        sb.append("</ul>");
        jw.write(sb.toString());
    }
    //当前页
    private void everyPage(StringBuilder sb) {
        if(paging.getTotalPages()>1){
            for(int i=1;i<=paging.getTotalPages();i++){
                sb.append("<li calss='"+(paging.getThisPage()==i?"active":"")+"'>");
                sb.append("<a href='"+getHref(i)+"'>");
                sb.append(i);
                sb.append("</a>");
                sb.append("</li>");
            }
        }else{

        }

    }

    //下一页
    private void nextPage(StringBuilder sb) {
        sb.append("<li class='"+(paging.isHasNext()?"":"disabled")+"'>");
        sb.append("<a href='"+(paging.isHasNext()?getHref(paging.getNext()):getHref(paging.getTotalPages()))+"' aria-label='Previous' aria-label='下一页'>");
        sb.append("<span aria-hidden='true'>");
        sb.append("«");
        sb.append("</span>");
        sb.append("</a>");
        sb.append("</li>");
    }

    //上一页
    private void previousPage(StringBuilder sb) {
        sb.append("<li class='"+(paging.isHasPrevious()?"":"disabled")+"'>");
        sb.append("<a href='"+(paging.isHasPrevious()?getHref(paging.getPrevious()):getHref(1))+"' aria-label='Previous' aria-label='上一页'>");
        sb.append("<span aria-hidden='true'>");
        sb.append("«");
        sb.append("</span>");
        sb.append("</a>");
        sb.append("</li>");
    }
    //对于链接的生成
    private String getHref(int index){
        //获得request对象
        PageContext pageContext=(PageContext)this.getJspContext();
        HttpServletRequest request=(HttpServletRequest)pageContext.getRequest();
        //取出请求的地址
        String requestUri=request.getAttribute(REQUEST_URI_ATTRIBUTE_KEY).toString();
        //获得request中所有请求的参数
        Map<String,String[]> map=new HashMap<>(request.getParameterMap());
        //删除页码的参数，因为页码的参数是拼接的，页码的参数名为index
        map.remove("thisPage");
        //开始拼接参数
        StringBuilder sb=new StringBuilder();
        for(Map.Entry<String,String[]> me:map.entrySet()){
            if(me.getValue().length>1){
                for(int i=0;i<me.getValue().length;i++){
                    sb.append(me.getKey()+"="+me.getValue()[i]+"&");
                }
            }else{
                sb.append(me.getKey()+"="+me.getValue()[0]+"&");
            }
        }
        if(sb.length()>1){
            sb.deleteCharAt(sb.length()-1);
        }
        if(sb.length()>0){
            return requestUri+"?thisPage="+index+"&"+sb.toString();
        }
        return requestUri+"?thisPage="+index;
    }
}
